package com.example.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.education.Bean.ForeignCollegeInfo;
import com.example.education.Bean.StudentInfo;
import com.example.education.Mapper.ForeignCollegeInfoMapper;
import com.example.education.service.ForeignCollegeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 姚
 * @since 2023-03-27
 */
@Service
@Transactional
public class ForeignCollegeInfoServiceImpl extends ServiceImpl<ForeignCollegeInfoMapper, ForeignCollegeInfo> implements ForeignCollegeInfoService {

    @Autowired
    private ForeignCollegeInfoMapper foreignCollegeInfoMapper;

    @Override
    public int insert(ForeignCollegeInfo foreignCollegeInfo) {
        if (foreignCollegeInfo.getUid() == null) {
            return foreignCollegeInfoMapper.insert(foreignCollegeInfo);
        } else {
            UpdateWrapper<StudentInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("uid", foreignCollegeInfo.getUid());

            //抄来的之后有需求再改
            updateWrapper.set("grade", foreignCollegeInfo.getCollegeName());


            return foreignCollegeInfoMapper.update(foreignCollegeInfo, updateWrapper);
        }

    }

    @Override
    public int delete(Integer uid) {
        return foreignCollegeInfoMapper.delete(uid);
    }

    @Override
    public int update(ForeignCollegeInfo foreignCollegeInfo) {
        if (foreignCollegeInfo.getUid() == null) {
            return foreignCollegeInfoMapper.insert(foreignCollegeInfo);
        } else {
            UpdateWrapper<StudentInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("uid", foreignCollegeInfo.getUid());

            //抄来的之后有需求再改
            updateWrapper.set("grade", foreignCollegeInfo.getCollegeName());


            return foreignCollegeInfoMapper.update(foreignCollegeInfo, updateWrapper);
        }
    }

    //根据学校名字查询这个学校
    @Override
    public ForeignCollegeInfo select(String collegename) {
        return foreignCollegeInfoMapper.selectByName(collegename);
    }

    //根据地区查所有这个地区的学
    @Override
    public ArrayList<ForeignCollegeInfo> selectByLocation(String location) {
        return foreignCollegeInfoMapper.selectByLocation(location);
    }


    //根据传来的平均分数查询学校
    @Override
    public ArrayList<ForeignCollegeInfo> selectByAverageScore(StudentInfo studentInfo) {

        QS qs = new QS();
        double score = qs.selectByScore(studentInfo.getAverageScore());
        ArrayList<ForeignCollegeInfo> foreignCollegeInfos = new ArrayList<>();
        //意向国家
        String country = "英国";//studentInfo.getTargetForeign();

        ForeignCollegeInfo foreignCollegeInfo = new ForeignCollegeInfo();
        //如果qs+50有相应学校并且这个学校在学生的意向国家之中 将这个学校加入到list中
        for (int i = 0; i < 50; i++) {
            foreignCollegeInfo = foreignCollegeInfoMapper.selectByQsRanking(score + i);
            if (foreignCollegeInfo != null && foreignCollegeInfo.getLocation().equals(country)) {
                foreignCollegeInfos.add(foreignCollegeInfo);
            }
        }

        //如果qs-30有相应学校并且这个学校在学生的意向国家之中 将这个学校加入到list中
        for (int i = 0; i < 30; i++) {
            foreignCollegeInfo = foreignCollegeInfoMapper.selectByQsRanking(score - i);
            if (foreignCollegeInfo != null && foreignCollegeInfo.getLocation().equals(country)) {
                foreignCollegeInfos.add(foreignCollegeInfo);
            }
        }

        System.out.println("$$$$$$$$$$$$$$$$$$$$$" + foreignCollegeInfos);
        return foreignCollegeInfos;
    }
}
